$(function() {
	$('.ranks').hide();
	var h1 = $('#stories').height();
	var h2 = $('#users').height();
//	$('.ranks').height( Math.max(h1,h2) );
    $('#stories').show();
    
    $('select').change(function() {   
        var val = $(this).val();
        if (val == "stories") {
            $('#users').slideUp();
//            $('#viewed').slideDown();
        }
        if (val == "users") {
            $('#stories').slideUp();
//            $('#voted').slideDown();
          }
        $('#' + val).slideDown();
        getSubUpdates();
    });
});

function subUpdateInit() {
	getSubUpdates();
	window.setInterval(getSubUpdates, 1000 );
}

function getSubUpdates() {
	var select = document.getElementById('sortBy');
	if (select.getElementsByTagName("option")[select.selectedIndex].value == "stories") {		
		$.getJSON("/writeonproject/update?page=story_subs", storySubscriptionsCallBack);
	} else {
		$.getJSON("/writeonproject/update?page=user_subs", userSubscriptionsCallBack);		
	}
}

function storySubscriptionsCallBack(data) {
	if( data != null && data.error != null && data.error == "false" ) {
		var storybody = document.getElementById('story_subs');
		if( data.stories.length > 0 ) {
			storybody.innerHTML = "";
			for( var i = 0; i < data.stories.length; i++ ) {
				var story = data.stories[i];
				var storytime = document.getElementById( "time" + story.storyid );
				if( storytime == null ) {
					var bottomRow = storybody.insertRow ( 0 );
					var topRow = storybody.insertRow( 0 );
					
					topRow.className = "rowon story1";
					var topcell1 = topRow.insertCell( 0 );
					var topcell2 = topRow.insertCell( 1 );
					var topcell3 = topRow.insertCell( 2 );
					var topcell4 = topRow.insertCell( 3 );
					var topcell5 = topRow.insertCell( 4 );
					
					topcell1.innerHTML = "<a href=\"/writeonproject/home?page=story_view&storyid=" + story.storyid + "\" class=\"title\">" + story.storytitle + "</a>";
					topcell2.className = "small_date";
					topcell2.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.authorid + "\" class=\"author\">" + story.authorid + "</a><br>" + story.createdate;
					topcell3.className = "story_post";
					topcell3.id = "pcount" + story.storyid;
					topcell3.innerHTML = story.numpost;
					topcell4.className = "small_date";
					topcell4.id = "time" + story.storyid;
					topcell4.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.lastposterid + "\" class=\"author\">" + story.lastposterid + "</a><br>" + story.timestamp;
					topcell5.rowSpan = "2";
					topcell5.className = "bottom_bor center_col_v center_col";
					topcell5.innerHTML = "<a href=\"\" class=\"button shrink\" onClick=\"unsubscribeStory(" + story.storyid + "); return false;\"><span><span>Unsubscribe</span></span></a>";
					
					bottomRow.className = "rowon";
					var bottom1 = bottomRow.insertCell( 0 );
					bottom1.className = "story_preview";
					bottom1.id = "sp" + story.storyid;
					bottom1.colSpan = "4";
					bottom1.innerHTML = story.preview;
				} else {
					var storyts = document.getElementById( "time" + story.storyid );
					storyts.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.lastposterid + "\" class=\"author\">" + story.lastposterid + "</a><br>" + story.timestamp; 
					
					var preview = document.getElementById( "sp" + story.storyid );
					preview.innerHTML = story.preview;
					
					var storyp = document.getElementById( "pcount" + story.storyid );
					storyp.innerHTML = story.numpost ;
				}
				
				document.getElementById("nostories").innerHTML = "";
			}
		} else {
			document.getElementById("nostories").innerHTML = "You have not subscribed to any stories yet!";
		}
	} else {
		alert(e.message);
	}
}

function userSubscriptionsCallBack(data) {
	if( data != null && data.error != null && data.error == "false" ) {
		var userbody = document.getElementById('user_subs');
		if( data.users.length > 0 ) {
			userbody.innerHTML = "";
			
			for( var i = 0; i < data.users.length; i++ ) {
				var user = data.users[i];
				
				var topRow = userbody.insertRow(0);

				topRow.className = "rowon story1";
				var topcell1 = topRow.insertCell(0);
				var topcell2 = topRow.insertCell(1);
				var topcell3 = topRow.insertCell(2);
				var topcell4 = topRow.insertCell(3);

				topcell1.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid="
						+ user.userid
						+ "\" class=\"title\">"
						+ user.userid + "</a>";
				topcell1.className = "bottom_bor center_col_v";
				topcell2.innerHTML = user.datecreated;
				topcell2.className = "bottom_bor center_col_v";
				
				var lastupdated = user.lastupdated;
				if (lastupdated == null || lastupdated == "undefined") {
					lastupdated = "Never";
				}
				topcell3.innerHTML = lastupdated;
				topcell3.className = "bottom_bor center_col_v";
				
				topcell4.className = "bottom_bor center_col_v center_col";
				topcell4.innerHTML = "<a href=\"\" class=\"button shrink\" onClick=\"unsubscribeUser('" + user.userid + "'); return false;\"><span><span>Unsubscribe</span></span></a>";
			}
			
			document.getElementById("nousers").innerHTML = "";
		} else {
			document.getElementById("nousers").innerHTML = "You have not subscribed to any users!";
		}
	} else {
		alert(e.message);
	}
}

function unsubscribeUser(userid) {
	$.getJSON("/writeonproject/update?page=unsubscribe_user&userid=" + userid, unsubscribeCallBack);	
}

function unsubscribeCallBack(data) {
	if (data != null && data.error != null && data.error == "true") {
		alert(data.reason);
	}
	
	getSubUpdates();
}

function unsubscribeStory(storyid) {
	$.getJSON("/writeonproject/update?page=unsubscribe_story&storyid=" + storyid, unsubscribeCallBack);		
}